	 
package c.x.jy.admin.oauth2;

import java.util.List;
import java.util.Set;

import c.x.jy.dao.entity.SysRoleEntity;
import c.x.jy.service.service.SysRoleService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import c.x.jy.common.utils.Constant;
import c.x.jy.dao.entity.SysUserEntity;
import c.x.jy.service.redis.Cache;
import c.x.jy.service.service.ShiroService;

/**
 * 认证
 *
 * @author 
 */
@Component
public class OAuth2Realm extends AuthorizingRealm {
    @Autowired
    private ShiroService shiroService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private   Cache redisUtils;
    
    private Integer EXPIRE=3600*24;

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof OAuth2Token;
    }

    /**
     * 授权(验证权限时调用)
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    	SysUserEntity user = ( SysUserEntity)principals.getPrimaryPrincipal();
        Long userId = user.getUserId();

        //用户权限列表
        Set<String> permsSet = shiroService.getUserPermissions(userId);

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setStringPermissions(permsSet);
        Set<String> roles=	sysRoleService.selectRolesByUserId(user.getUserId());
        info.setRoles(roles);
        return info;
    }

    /**
     * 认证(登录时调用)
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
         String accessToken = (String) token.getPrincipal();
      // System.out.println("accessToken:"+accessToken);
        //根据accessToken，查询用户信息
         SysUserEntity user = shiroService.queryByToken(accessToken);
  	 // System.out.println(user);
		  //token失效
		  if(user == null  ){ 
			  System.out.println("token失效，请重新登录");
			  throw new  IncorrectCredentialsException("token失效，请重新登录");
			  }
		 
		 
        //查询用户信息
        //  user = shiroService.queryUser(user.getUserId());
        //账号锁定
        if(user.getStatus() ==null||user.getStatus() == 0){
        	  System.out.println("账号已被锁定,请联系管理员");
            throw new LockedAccountException("账号已被锁定,请联系管理员");
        }
        user.setPassword (null);
        user.setSalt( null); 
    	redisUtils.set(accessToken, user, Long.valueOf(EXPIRE));
    	redisUtils.set(Constant.USERKEY+user.getUserId()+":"+accessToken, accessToken, Long.valueOf(EXPIRE));
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, accessToken, getName());
        return info;
    }
}
